package com.corpgovernment.organization.util;

import com.corpgovernment.common.utils.DateUtil;
import com.ctrip.corp.obt.generic.utils.StringUtils;

import java.util.Calendar;
import java.util.Date;

public class BudgetDateUtil {

    /**
     * 设置日期为当前月第一天
     *
     * @param time yyyy-MM
     * @return yyyy-MM-01
     */
    public static Date getFirstDayOfMonth(String time) {
        Date date = DateUtil.stringToDate(time, DateUtil.DF_YM);
        PreCheckUtil.checkNotNull(date, "时间格式错误");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 设置日期为当前月最后一天
     *
     * @param time yyyy-MM
     * @return yyyy-MM-28/29/30/31
     */
    public static Date getLastDayOfMonth(String time) {
        Date date = DateUtil.stringToDate(time, DateUtil.DF_YM);
        PreCheckUtil.checkNotNull(date, "时间格式错误");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 设置月份、日期为当前年的第一天
     *
     * @param time yyyy
     * @return yyyy-01-01
     */
    public static Date getFirstDayOfYear(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        Date date = DateUtil.stringToDate(time, DateUtil.DF_Y);
        PreCheckUtil.checkNotNull(date, "预算时间格式错误");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 设置月份、日期为当前年的最后一天
     *
     * @param time yyyy
     * @return yyyy-12-31
     */
    public static Date getLastDayOfYear(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        Date date = DateUtil.stringToDate(time, DateUtil.DF_Y);
        PreCheckUtil.checkNotNull(date, "预算时间格式错误");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }
}